import 'package:flutter/cupertino.dart';
import 'package:gameplay_flutter/lib_basemodel/user/user.dart';
import 'package:gameplay_flutter/lib_network/http_helper.dart';
import 'package:gameplay_flutter/m_me/me_api.dart';
import 'package:get/get.dart';

import '../../lib_basemodel/discover/moment_entity.dart';
import '../model/me_menu.dart';
import '../model/room_card.dart';

class UserDetailsPageController extends GetxController {
  var user = User().obs;
  var moments = <MomentEntity>[].obs;
  var likeMoments = <MomentEntity>[].obs;
  var roomCard = RoomCard().obs;

  @override
  void onInit() async {
    super.onInit();
  }

  getUserDetails(String userId) async {
    var res = await HttpHelper.instance().post(
      MeApi.userDetail,
      body: {"userId": userId},
      showLoading: true,
      showToast: true,
    );
    if (res.result is Map<String, dynamic>) {
      User details = User.fromJson(res.result);
      user.value = details;
    } else {
      debugPrint("res.result 不是有效的 JSON 数据");
    }
  }

  getUserDynamic(
    String userId,
    int pageNum,
  ) async {
    var res = await HttpHelper.instance().post(
      MeApi.userDynamic,
      body: {
        "userId": userId,
        "pageNum": pageNum,
        "pageSize": 20,
        "orderType": "personalTop_createTime"
      },
      showLoading: true,
      showToast: true,
    );
    if (res.result["dynamicList"] is List) {
      if (pageNum == 1) {
        moments.clear();
      }
      for (var item in res.result["dynamicList"]) {
        if (item['dynamicVO'] is Map<String, dynamic>) {
          var moment = MomentEntity.fromJson(item['dynamicVO']);
          moments.add(moment);
        } else {
          debugPrint("item['dynamicVO'] 不是有效的 JSON 数据");
        }
      }
      // 现在 dtos 是一个包含 FollowingMomentDtoEntity 对象的列表
      // 你可以在这里对 dtos 进行进一步处理
    } else {
      debugPrint("res.result 不是有效的 JSON 数组");
    }
  }

  getLikeDynamic(
    String userId,
    int pageNum,
  ) async {
    var res = await HttpHelper.instance().post(
      MeApi.userLikeDynamic,
      body: {
        "userId": userId,
        "pageNum": pageNum,
        "pageSize": 20,
        "orderType": "personalTop_createTime"
      },
      showLoading: true,
      showToast: true,
    );
    if (res.result is List) {
      if (pageNum == 1) {
        likeMoments.clear();
      }
      for (var item in res.result) {
        if (item is Map<String, dynamic>) {
          var moment = MomentEntity.fromJson(item);
          likeMoments.add(moment);
        } else {
          debugPrint("item['dynamicVO'] 不是有效的 JSON 数据");
        }
      }
      // 现在 dtos 是一个包含 FollowingMomentDtoEntity 对象的列表
      // 你可以在这里对 dtos 进行进一步处理
    } else {
      debugPrint("res.result 不是有效的 JSON 数组");
    }
  }

  isAttention(String userId, bool isAttention) async {
    var res = await HttpHelper.instance().post(
      MeApi.isAttention,
      body: {"toUserId": userId, "isAttention": isAttention ? "0" : "1"},
      showLoading: true,
      showToast: true,
    );
    if (res.isSuccess) {
      user.update((val) {
        val?.isAttention = !isAttention;
      });
    }
    if (res.result is Map<String, dynamic>) {
    } else {
      debugPrint("res.result 不是有效的 JSON 数据");
    }
  }

  deleteDynamic(String id) async {
    var res = await HttpHelper.instance().post(
      MeApi.deleteDynamic,
      body: {"id": id},
      showLoading: true,
      showToast: true,
    );
    if (res.isSuccess) {
      moments.value = moments.toList()
        ..removeWhere((element) => element.id.toString() == id);
    }
  }

  getRoomCard(String id) async {
    var res = await HttpHelper.instance().post(
      MeApi.roomCard,
      body: {"userId": id},
      showLoading: true,
      showToast: false,
    );
    if (res.isSuccess && res.result is Map<String, dynamic>) {
      roomCard.value = RoomCard.fromJson(res.result);
    }
  }
}
